<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object</title>
</head>
<body>
    <script>
        // 定义
        let boy = {
            name: "老大",
            gender: "男",
            age: 45,
            weight: 60.5,
            web: "https://www.laoda.com"
        };
        // Object 对象没有 prototype 属性
        console.log("typeof, ", typeof boy);

        console.log("instanceof Object, ", boy instanceof Object);

        console.log("boy: ", boy);

        // 增加属性
        boy.height = 170;
        console.log("boy: ", boy);

        // 修改属性
        boy.web = "https://www.126.com";
        console.log("boy: ", boy);

        // 删除属性
        delete boy.weight;
        console.log("boy: ", boy);

        // 判断属性是否存在
        let has = "age" in boy;
        console.log("has: ", has);

        // 对象属性名数组
        console.log(Object.keys(boy));
        
        // for...in 遍历对象可枚举属性
        for (let k in boy) {
            console.log("for...in key: ", k, ", value: ", boy[k]);
        }
        
        // Object.entries，解构对象 [k, v]
        Object.entries(boy).forEach(([k, v]) => {
            console.log("forEach key: ", k, ", value: ", v);
        });

        // 清空对象
        boy = {};
        console.log("boy: ", boy);

        // 私有属性、存取器 get set
        class Person {
            name;
            gender;
            #age; // 私有属性
            constructor(name, age, gender) {
                this.name = name;
                this.#age = age;
                this.gender = gender;
            }
            // 可以访问私有属性
            get age() {
                return this.#age;
            }
            // 可以修改私有属性
            set age(age) {
                this.#age = age;
            }

            info() {
                // console.log("name=" + this.name + ", age=" + this.age);
                // 模板字符串
                return `姓名：${this.name}，年龄：${this.#age}`;
            }

            sleep() {
                return `${this.name} 正在休息中...`;
            }
        }
        let p = new Person("laoda", 45);
        console.log(p.info());
        console.log("非私有属性 name ：", p.name);
        console.log("私有属性 age ：", p.age);

        // 继承
        class Student extends Person {
            web;

            constructor(name, age, gender, web) {
                super(name, age, gender);
                this.web = web;
            }

            study() {
                return `${this.name} 正在学习中...`;
            }
        }
        let stu = new Student("laoda", 45, "男", "https://www.laoda.com");
        console.log(stu.info());
        console.log(stu.sleep());
        console.log(stu.study());
        
    </script>
</body>
</html>